package com.ece.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.ece.beans.users.Utilisateur;
import com.ece.control.GeneralController;
import com.ece.control.ItemController;

public class KiwiMainFrame extends JFrame {

	private final JFrame frame = new JFrame();
	private final CartPanel cart;
	private final CategoryPanel category;
	private final ItemListPanel itemList;
	private final JButton buttonAddCart;
	private final JButton buttonRemCart;
	private final JButton buttonSendCart;
	private final JButton buttonDisconnect = new JButton("Disconnect");

	private final Utilisateur utilisateur;

	public KiwiMainFrame(Utilisateur utilisateur) {
		GeneralController.getInstance().addContainer(KiwiMainFrame.class.getName(), this);

		this.utilisateur = utilisateur;

		this.frame.setSize(800, 800);
		this.frame.setLayout(new BorderLayout());

		buttonAddCart = new JButton("Add to Cart");
		buttonAddCart.setMaximumSize(new Dimension(50, 25));
		buttonAddCart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ItemController.addItemToCart();
			}
		});

		buttonRemCart = new JButton("Remove from Cart");
		buttonRemCart.setMaximumSize(new Dimension(50, 25));
		buttonRemCart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ItemController.RemoveItemFromCart();
			}
		});

		buttonSendCart = new JButton("Command");
		buttonSendCart.setMaximumSize(new Dimension(50, 25));
		buttonSendCart.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ItemController.sendCommande();
			}
		});

		buttonDisconnect.setMaximumSize(new Dimension(50, 25));
		buttonDisconnect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				frame.setVisible(false);
				new LoginFrame().show();
			}
		});

		category = new CategoryPanel();

		itemList = new ItemListPanel();
		itemList.add(buttonAddCart, BorderLayout.SOUTH);

		cart = new CartPanel();
		cart.add(buttonRemCart, BorderLayout.SOUTH);

		JPanel pane = (JPanel) this.frame.getContentPane();
		pane.add(category, BorderLayout.WEST);
		pane.add(itemList, BorderLayout.CENTER);
		pane.add(cart, BorderLayout.EAST);	

		JPanel panelBottom = new JPanel(new FlowLayout());
		panelBottom.add(buttonDisconnect);
		panelBottom.add(buttonSendCart);

		pane.add(panelBottom, BorderLayout.PAGE_END);
	}

	@Override
	public void show() {
		this.frame.setVisible(true);
	}

	@Override
	public void hide() {
		this.frame.setVisible(false);
	}

	public JFrame getFrame() {
		return frame;
	}

	public CartPanel getCart() {
		return cart;
	}

	public CategoryPanel getCategory() {
		return category;
	}

	public ItemListPanel getItemList() {
		return itemList;
	}

	public Utilisateur getUtilisateur() {
		return this.utilisateur;
	}
}
